package org.csu.mypetstore.service;

//import org.mybatis.jpetstore.domain.Account;
//import org.mybatis.jpetstore.persistence.AccountMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.persistence.AccountDAO;
import org.csu.mypetstore.persistence.impl.AccountDAOImpl;

//@Service
public class AccountService {

//  @Autowired
  private AccountDAO accountDAO;

  //构造函数
  public AccountService() {
    accountDAO = new AccountDAOImpl();
  }

  public Account getAccount(String username) {
    return accountDAO.getAccountByUsername(username);
  }

  public Account getAccount(String username, String password) {
    Account account = new Account();
    account.setUsername(username);
    account.setPassword(password);
    return accountDAO.getAccountByUsernameAndPassword(account);
  }

//  @Transactional
  public void insertAccount(Account account) {
    accountDAO.insertAccount(account);
    accountDAO.insertProfile(account);
    accountDAO.insertSignon(account);
  }

//  @Transactional
  public void updateAccount(Account account) {
    accountDAO.updateAccount(account);
    accountDAO.updateProfile(account);
    account.setBannerName(accountDAO.getBannerNameByFavouriteCategoryId(account.getFavouriteCategoryId()));

    if (account.getPassword() != null && account.getPassword().length() > 0) {
      accountDAO.updateSignon(account);
    }
  }

}
